package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    /**
     * 根据分类id查询规格组
     * @param cid   分类id
     * @return      返回规格组的DTO集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategoryId(@RequestParam("id") Long cid){
        return ResponseEntity.ok(specService.querySpecGroupByCategoryId(cid));
    }

    /**
     * 查询规格参数
     * @param gid   规格组id
     * @param cid   分类id
     * @param searching 是否查询
     * @return      规格参数的dto集合
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParam(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching
    ){
        return ResponseEntity.ok(specService.querySpecParam(gid, cid, searching));
    }

    /**
     * 根据分类的id查询规格组和组内参数
     * @param id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecByCategoryId(@RequestParam("id") Long id){
        List<SpecGroupDTO> groupDTOList = specService.querySpecByCategoryId(id);
        return ResponseEntity.ok(groupDTOList);
    }
}
